package cz.data.domain.masterdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.masterdata.model.dto.ModelData;
import cz.data.domain.masterdata.model.query.ModelDataQuery;
import cz.data.domain.masterdata.service.ModelDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class ModelDataController extends BaseController {

    @Autowired
    private ModelDataService modelDataService;

    @GetMapping("/datas/{id}")
    public SimpleDataResponse<JSONObject> getModelDataById(@PathVariable String id, @Validated({ValidationGroups.Update.class}) ModelData modelData) {
        JSONObject data = modelDataService.getModelDataById(modelData);
        return new SimpleDataResponse<>(data);
    }

    @PostMapping("/datas/page")
    public SimpleDataResponse<JsonPage<JSONObject>> getPageModelDatas(@RequestBody @Valid ModelDataQuery modelDataQuery) {
        IPage<JSONObject> page = modelDataService.getPageModelDatas(modelDataQuery);
        JsonPage<JSONObject> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
        return new SimpleDataResponse<>(jsonPage);
    }

    @PostMapping("/datas/addData")
    public BaseResponse addModelData(@RequestBody @Valid ModelData modelData) {
        modelDataService.addModelData(modelData);
        return BaseResponse.success();
    }

    @PutMapping("/datas/updateData/{id}")
    public BaseResponse updateModelData(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ModelData modelData) {
        modelDataService.updateModelData(modelData);
        return BaseResponse.success();
    }

    @PostMapping("/datas/delData/{id}")
    public BaseResponse deleteModelData(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ModelData modelData) {
        modelDataService.delModelData(modelData);
        return BaseResponse.success();
    }
}
